import Vue from 'vue'
import VueRouter from 'vue-router'
import { getToken } from '@/utils/TokenOpers'
import { userRouterMap,autoSCMap } from '@/router/routerConfig'
Vue.use(VueRouter)

const routes = [
  ...userRouterMap,
  ...autoSCMap
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})


//路由监听
import store from '@/store'
import { Message } from 'ant-design-vue';
import { Modal } from 'ant-design-vue'
import { setStore,getStore,removeStore } from '@/utils/StoreOpers'
import { deleteModel } from '@/axios/api'
router.beforeEach(async (to, from, next) => {
  Modal.destroyAll();
  const hasToken = getToken()
  const a = from.path
  //没有token的处理
  if(!hasToken && from.path !== '/user/login'  && to.path !== '/user/login'){
    if (!hasToken) {
      Message.error("请先登录")
      next('/user/login')
    } else {
      next({ ...to, replace: true })
    }
  }
  //建模页面跳出的处理
  const {name,params} = from
  if(['amByList','amByGraph'].includes(name)){
    const FROM_MODEL = params.model//从哪儿跳出来的?
    const TO_MODEL = to.params.model
    if (TO_MODEL && FROM_MODEL===TO_MODEL){
    }else{
      if(
        // RUNING_TAG.filter(e=>e!=null).length>0 || //有执行
        store.getters.NEED_SAVE //需要保存
      ){
        const {id} = store.getters.modelInfo
        await setStore(FROM_MODEL,id)
      }
      store.dispatch('RESET',{all:true})
    }
    // store.commit('setModel',null)
  }
  next()
})
router.afterEach((to)=>{
  console.log({to})
  const { name,params,meta } = to
  let title
  if(['amByList','amByGraph'].includes(name))
    title = params.model+'模型'
  else title = meta.title
  document.title = `自动建模系统-${title}`
  store.commit('SET_TITLE',title)
})
export default router
